home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 16.1 KB | 486 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWPStr.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef FWPSTR_H
- #include "FWPStr.h"
- #endif
-
- #ifndef SLSTRREP_H
- #include "SLStrRep.h"
- #endif
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- #ifndef FWMEMHLP_H
- #include "FWMemHlp.h"
- #endif
-
- #ifndef FWEXCEPT_H
- #include "FWExcept.h"
- #endif
-
- //========================================================================================
- // File scope definitions
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment Strings
- #endif
-
- //========================================================================================
- // CLASS FW_CString
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CString)
- FW_DEFINE_CLASS_M0(FW_CString)
-
- // This class is archivable, but we provide the archiving implementation in a separate
- // translation unit in order to enable deadstripping of the archiving-related code
- // in parts that do not use archiving with this class.
-
- //----------------------------------------------------------------------------------------
- // FW_CString::~FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::~FW_CString()
- {
- FW_START_DESTRUCTOR
- FW_PrivString_Release(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString()
- : fRep(FW_PrivString_AcquireEmptyString())
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString(ODIText *text)
- : fRep(FW_PrivString_AcquireEmptyString())
- {
- ReplaceAll(text);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString(FW_HString rep)
- : fRep(rep)
- {
- FW_PrivString_Acquire(fRep);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString(const FW_CString& other)
- : fRep(other.fRep)
- {
- FW_PrivString_Acquire(fRep);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString(const char* string)
- : fRep(FW_PrivString_AcquireEmptyString())
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_ReplaceAllBytes(fRep, string, FW_PrimitiveStringLength(string), &error);
- FW_FailOnError(error);
- fRep = rep;
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString(const char* bytes, FW_ByteCount byteLength)
- : fRep(FW_PrivString_AcquireEmptyString())
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_ReplaceAllBytes(fRep, bytes, byteLength, &error);
- FW_FailOnError(error);
- fRep = rep;
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
- FW_CString::FW_CString(const char* string, const FW_Locale& locale)
- : fRep(0)
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_AcquireEmptyStringWithLocale(locale, &error);
- FW_FailOnError(error);
- rep = FW_PrivString_ReplaceAllBytes(rep, string, FW_PrimitiveStringLength(string), &error);
- FW_FailOnError(error);
- fRep = rep;
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::FW_CString(const char* bytes, FW_ByteCount byteLength, const FW_Locale& locale)
- : fRep(0)
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_AcquireEmptyStringWithLocale(locale, &error);
- FW_FailOnError(error);
- rep = FW_PrivString_ReplaceAllBytes(rep, bytes, byteLength, &error);
- FW_FailOnError(error);
- fRep = rep;
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::FW_CString
- //----------------------------------------------------------------------------------------
-
- FW_CString::operator FW_HString*()
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_LockString(fRep, &error);
- FW_FailOnError(error);
- fRep = rep;
- return &fRep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator[]
- //----------------------------------------------------------------------------------------
-
- FW_LChar FW_CString::operator[](FW_CharacterPosition position) const
- {
- FW_Locale locale;
- GetLocale(locale);
- if (FW_LocaleIsSingleByte(locale))
- {
- char ch;
- FW_PrivString_Retrieve(fRep, &ch, 1, position);
- return ch;
- }
-
- // Get the byte position
- FW_PlatformError error = 0;
- ODIText* tp = RevealODIText();
- FW_BytePosition bytePosition = FW_TextParams_GetBytePosition(tp, position, &error);
- FW_FailOnError(error);
-
- if (!FW_TextParams_IsCharacterStart(tp, 0, bytePosition, &error))
- {
- // second of two-byte char - position must be wrong
- error = FW_xUnknownError;
- }
- else // either first of two-byte, or single-byte character
- {
- // Check the next byte. If it's also the first, we know that our character is a single
- if (bytePosition+1 >= GetByteLength() || FW_TextParams_IsCharacterStart(tp, 0, bytePosition+1, &error))
- {
- char ch;
- FW_PrivString_Retrieve(fRep, &ch, 1, bytePosition);
- return ch;
- }
- }
- FW_FailOnError(error);
-
- // Retrieve a two-byte character
- FW_LChar theChar;
- FW_PrivString_Retrieve(fRep, (char*)&theChar, 2, bytePosition);
- return theChar;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::operator=
- //----------------------------------------------------------------------------------------
-
- FW_CString& FW_CString::operator=(const FW_CString& other)
- {
- if (&other != this)
- {
- ReplaceAll(other);
- }
- return *this;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::GrowCapacity
- //----------------------------------------------------------------------------------------
-
- FW_ByteCount FW_CString::GrowCapacity(FW_ByteCount numberBytes)
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_SetCapacity(fRep, numberBytes, &error);
- FW_FailOnError(error);
- fRep = rep;
- return FW_PrivString_GetCapacity(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::PrivRead
- //----------------------------------------------------------------------------------------
-
- FW_CReadableStream& FW_CString::PrivRead(FW_CReadableStream& stream)
- {
- FW_Locale locale;
-
- stream >> locale.fScriptCode >> locale.fLangCode;
-
- short byteCount;
- stream >> byteCount;
-
- FW_CAcquireTemporarySystemHandle handle(byteCount);
- char* buffer = (char*) handle.GetPointer();
-
- stream.Read(buffer, byteCount);
- FW_CString temp(buffer, byteCount, locale);
-
- ReplaceAll(temp);
-
- return stream;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::PrivWrite
- //----------------------------------------------------------------------------------------
-
- FW_CWritableStream& FW_CString::PrivWrite(FW_CWritableStream& stream) const
- {
- FW_Locale locale;
- GetLocale(locale);
-
- stream << locale.fScriptCode << locale.fLangCode;
-
- FW_ByteCount longByteCount = GetByteLength();
- FW_ASSERT(longByteCount < 32768L);
- short byteCount = (short) longByteCount;
- stream << byteCount;
- const char* buffer = RevealBuffer();
- stream.Write(buffer, byteCount);
- return stream;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::GetCharacterLength
- //----------------------------------------------------------------------------------------
-
- FW_CharacterCount FW_CString::GetCharacterLength() const
- {
- FW_PlatformError error = 0;
- FW_CharacterCount result = FW_PrivString_GetCharacterLength(fRep, &error);
- FW_FailOnError(error);
- return result;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ParseAsSignedInteger
- //----------------------------------------------------------------------------------------
-
- long FW_CString::ParseAsSignedInteger() const
- {
- return FW_PrivString_DecimalStringToSignedInteger(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ParseAsUnsignedInteger
- //----------------------------------------------------------------------------------------
-
- unsigned long FW_CString::ParseAsUnsignedInteger() const
- {
- return FW_PrivString_DecimalStringToUnsignedInteger(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ParseAsHexadecimalInteger
- //----------------------------------------------------------------------------------------
-
- unsigned long FW_CString::ParseAsHexadecimalInteger() const
- {
- return FW_PrivString_HexadecimalStringToUnsignedInteger(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ParseAsRealNumber
- //----------------------------------------------------------------------------------------
-
- double FW_CString::ParseAsRealNumber() const
- {
- return FW_PrivString_StringToDouble(fRep);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ReplaceAllAsSignedDecimalInteger
- //----------------------------------------------------------------------------------------
-
- void FW_CString::ReplaceAllAsSignedDecimalInteger(long integer)
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_SignedIntegerToDecimalString(fRep, integer, &error);
- FW_FailOnError(error);
- fRep = rep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ReplaceAllAsUnsignedDecimalInteger
- //----------------------------------------------------------------------------------------
-
- void FW_CString::ReplaceAllAsUnsignedDecimalInteger(unsigned long integer)
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_UnsignedIntegerToDecimalString(fRep, integer, &error);
- FW_FailOnError(error);
- fRep = rep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ReplaceAllAsHexadecimalInteger
- //----------------------------------------------------------------------------------------
-
- void FW_CString::ReplaceAllAsHexadecimalInteger(unsigned long integer)
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_UnsignedIntegerToHexadecimalString(fRep, integer, &error);
- FW_FailOnError(error);
- fRep = rep;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CString::ReplaceAllAsRealNumber
- //----------------------------------------------------------------------------------------
-
- void FW_CString::ReplaceAllAsRealNumber(double real, short fracDigits)
- {
- FW_PlatformError error = 0;
- FW_HString rep = FW_PrivString_DoubleToString(fRep, real, fracDigits, &error);
- FW_FailOnError(error);
- fRep = rep;
- }
-
- //========================================================================================
- // CLASS FW_CAcquireNulTerminatedString
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CAcquireNulTerminatedString)
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireNulTerminatedString::FW_CAcquireNulTerminatedString
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireNulTerminatedString::FW_CAcquireNulTerminatedString(const FW_CString& string)
- : fExportedString(NULL)
- {
- fExportedString = new char[string.GetByteLength()+1];
- string.ExportCString(fExportedString);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireNulTerminatedString::~FW_CAcquireNulTerminatedString
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireNulTerminatedString::~FW_CAcquireNulTerminatedString()
- {
- FW_START_DESTRUCTOR
- delete [] fExportedString;
- }
-
- //========================================================================================
- // CLASS FW_CAcquireNulTerminatedString255
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireNulTerminatedString255::FW_CAcquireNulTerminatedString255
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireNulTerminatedString255::FW_CAcquireNulTerminatedString255(const FW_CString& string)
- {
- FW_PRIV_ASSERT(string.GetByteLength() < sizeof(fExportedString));
- string.ExportCString(fExportedString);
- }
-
- //========================================================================================
- // Global FW_CString compare functions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // operator==
- //----------------------------------------------------------------------------------------
-
- FW_Boolean operator==(const FW_CString& string1, const FW_CString& string2)
- {
- return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringsEqual;
- }
-
- //----------------------------------------------------------------------------------------
- // operator!=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean operator!=(const FW_CString& string1, const FW_CString& string2)
- {
- return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringsEqual;
- }
-
- //----------------------------------------------------------------------------------------
- // operator<
- //----------------------------------------------------------------------------------------
-
- FW_Boolean operator<(const FW_CString& string1, const FW_CString& string2)
- {
- return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringOneLess;
- }
-
- //----------------------------------------------------------------------------------------
- // operator>
- //----------------------------------------------------------------------------------------
-
- FW_Boolean operator>(const FW_CString& string1, const FW_CString& string2)
- {
- return FW_PrivString_Compare(string1.fRep, string2.fRep) == FW_kStringOneGreater;
- }
-
- //----------------------------------------------------------------------------------------
- // operator<=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean operator<=(const FW_CString& string1, const FW_CString& string2)
- {
- return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringOneGreater;
- }
-
- //----------------------------------------------------------------------------------------
- // operator>=
- //----------------------------------------------------------------------------------------
-
- FW_Boolean operator>=(const FW_CString& string1, const FW_CString& string2)
- {
- return FW_PrivString_Compare(string1.fRep, string2.fRep) != FW_kStringOneLess;
- }
-
-